package com.nettylearning.base.netty.io.buffer;

import java.nio.ByteBuffer;

/**
 * @author vince
 * @date 2021/7/17 7:36
 * @description 5. 只读缓冲区
 * @Version 1.0
 */
public class ReadOnlyBuffer {
    public static void main(String[] args) {
        ByteBuffer buffer = ByteBuffer.allocate(10);

        // 缓冲区数据0-9
        for (int i = 0; i < buffer.capacity(); i++) {
            buffer.put((byte) i);
        }

        // 创建只读缓冲区
        ByteBuffer readonly = buffer.asReadOnlyBuffer();

        // 改变缓冲区的内容
        for (int i = 0; i < buffer.capacity(); i++) {
            byte b = buffer.get(i);
            b *= 10;
            buffer.put(i, b);
        }

        readonly.position(0);
        readonly.limit(buffer.capacity());

        // 只读缓冲区的内容随之改变
        while (readonly.remaining() > 0) {
            System.out.println(readonly.get());
        }


    }
}
